package x.trident.banner;

import org.springframework.boot.Banner;
import org.springframework.boot.ansi.AnsiColor;
import org.springframework.boot.ansi.AnsiOutput;
import org.springframework.boot.ansi.AnsiStyle;
import org.springframework.core.env.Environment;

import java.io.PrintStream;

/**
 * banner接口
 *
 * @author Seven
 * @date 2021-08-29
 */
public interface IBanner extends Banner {

    /**
     * banner实体
     *
     * @return banner info
     */
    public BannerEntity getBannerEntity();

    /**
     * 打印banner
     *
     * @param environment 运行环境
     * @param sourceClass sourceClass
     * @param printStream printStream
     */
    @Override
    default void printBanner(Environment environment, Class<?> sourceClass, PrintStream printStream) {

        BannerEntity bannerEntity = getBannerEntity();
        StringBuilder stringBuffer = bannerEntity.getBanner();

        String[] banner = stringBuffer.toString().split(",");

        for (String line : banner) {
            printStream.println(AnsiOutput.toString(AnsiColor.MAGENTA, line));
        }

        String version = bannerEntity.getVersion();
        printStream.println(AnsiOutput.toString(AnsiColor.GREEN, " :: Powered by " + bannerEntity.getCopyright() + ".inc :: ", AnsiColor.DEFAULT, AnsiStyle.FAINT, version));
        printStream.println();
    }

}
